import { DataTypes } from "sequelize";

export default function initSkuSpec(sequelize) {
  const SkuSpec = sequelize.define(
    "SkuSpec",
    {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      sku_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "sku",
          key: "id",
        },
      },
      spec_type_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "spec_type",
          key: "id",
        },
        comment: "规格类型ID（如颜色、尺寸）",
      },
      spec_value_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: "规格值ID（如红色的ID）",
      }
    },
    {
      tableName: "sku_spec_value",
      timestamps: false, // 若无需要可关闭时间戳
    }
  );

// 添加关联关系
  SkuSpec.associate = function(models) {
    SkuSpec.belongsTo(models.SpecValue, {
      foreignKey: 'spec_value_id',
      as: 'specValue'  // 这个别名需要和查询时保持一致
    });
    
    SkuSpec.belongsTo(models.SpecType, {
      foreignKey: 'spec_type_id',
      as: 'specType'
    });
  };

  return SkuSpec;
}
